/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.MainPanel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import pacificlms.DataHolder;
import pacificlms.MasterConnection;
import pacificlms.Verifiable.*;
import pacificlms.Verifiable.Database.*;
import pacificlms.Error.ErrorHandle;
import java.lang.reflect.Method;
import javax.swing.DefaultListModel;
import pacificlms.LoggedMember;

/**
 * InformationResource is responsible for displaying, editing, and creating
 * Resources (the InformationResource panel does not create copies the add copy button just opens a copy panel to do it)
 * @author Brian Hume
 */
public class InformationResource extends CentralUIPanel {

    /**
     * Creates new form InformationResource
     */
    public InformationResource(VerifiableObject VO) {
        super(null);
        initComponents();
        Create((Resource)VO);
    }
    
    private Resource CurrentResource = null;
    private boolean NewResource = true;
    /**
     * Creates the InformationResource Panel, Fills the combo box, and Fields, Sets the retreval Links, 
     * @param Res 
     */
    void Create(Resource Res)
    {
        CurrentResource = Res;
        if (Res == null)
            NewResource = true;
        else
            NewResource = false;
        FillResourceTypeList();
        FillFields();
        
        try
        {
            Method[] MethodList = this.getClass().getDeclaredMethods();
            
            for(Method ML: MethodList)
            {
                if (ML.getName().equals("SetResource"))
                    SetMethodRef = ML;
                if (ML.getName().equals("SetMember"))
                    SetMethodMemberRef = ML;
                if (ML.getName().equals("RefreshCopies"))
                    ResetCopiesRef = ML;
                System.out.println(ML.getName());
            }
            //Class[] Arglist = new Class[1];
            //Arglist[0] = VerifiableObject.class;
            //SetMethodRef = ReflectionHelper.getMethod this.getClass().getMethod("SetMethod", Arglist);
        }
        catch(Exception Ex)
        {
            System.out.println(Ex.toString());
            SetMethodRef = null;
        }
    }
    
    protected Method SetMethodRef;
    protected Method SetMethodMemberRef;
    protected Method ResetCopiesRef;
    
    public String TabName()
    {
        return "Resource Catalog";
    }
    
    /**
     * Fills all of the resource types from the DataHolder class
     */
    void FillResourceTypeList()
    {
        ArrayList<ResourceType> RTL = DataHolder.GetResourceTypeList();
        for(ResourceType RT: RTL)
        {
            cboResourceType.addItem(RT.Type(null));
        }
        cboResourceType.setSelectedIndex(0);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtResourceCode = new javax.swing.JTextField();
        cboResourceType = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtForiegnOwner = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtSubTitle = new javax.swing.JTextField();
        txtKeyword = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btnFindOwner = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        txtActors = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtSongs = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtIssueNumber = new javax.swing.JTextField();
        txtInformation = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstCopies = new javax.swing.JList();
        btnSearch = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        btnNew = new javax.swing.JButton();
        btnViewCopy = new javax.swing.JButton();
        btnAddCopy = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(615, 535));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 22));
        jLabel11.setText("Resource Information");

        jLabel1.setText("Type");

        jLabel2.setText("Resource Code");

        jLabel3.setText("Title");

        jLabel4.setText("Foreign Owner");

        jLabel6.setText("Copies");

        jLabel7.setText("Sub-Title");

        jLabel8.setText("Keyword List");

        btnFindOwner.setText("Search");
        btnFindOwner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindOwnerActionPerformed(evt);
            }
        });

        jLabel9.setText("Actors");

        jLabel10.setText("Songs");

        jLabel12.setText("Information");

        jLabel13.setText("Issue Number");

        jScrollPane1.setViewportView(lstCopies);

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnNew.setText("New Resource");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        btnViewCopy.setText("View Copy");
        btnViewCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewCopyActionPerformed(evt);
            }
        });

        btnAddCopy.setText("Add Copy");
        btnAddCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddCopyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtSubTitle))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel11))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtResourceCode, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtForiegnOwner, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnFindOwner, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(cboResourceType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addGap(0, 193, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtKeyword, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtActors, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtSongs, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtInformation, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtIssueNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 369, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnAddCopy, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnViewCopy, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnNew, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 175, Short.MAX_VALUE)
                                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtResourceCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSubTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtForiegnOwner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(btnFindOwner, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboResourceType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtActors, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSongs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtInformation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtIssueNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnViewCopy, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnAddCopy, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNew)
                    .addComponent(btnSearch)
                    .addComponent(btnSave)
                    .addComponent(btnClose))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Changes Null resource to New resource, Fills all of the fields, displays all copies, toggles controlable buttons
     */
    void FillFields()
    {
        if (CurrentResource == null)
        {
            CurrentResource = new Resource();
        }
        
        txtResourceCode.setText(CurrentResource.ResourceCode(null));
        txtTitle.setText(CurrentResource.Title(null));
        txtSubTitle.setText(CurrentResource.SubTitle(null));
        txtKeyword.setText(CurrentResource.Keywords(null));
        txtForiegnOwner.setText(CurrentResource.ForiegnOwner(null));
        
        txtActors.setText(CurrentResource.Actors(null));
        txtSongs.setText(CurrentResource.Songs(null));
        txtInformation.setText(CurrentResource.Information(null));
        txtIssueNumber.setText(CurrentResource.IssueNumber(null));
        
        

        SetResourceType(CurrentResource.Type(null));
        ListCopies();
        

        if (LoggedMember.getInstance().IsLibrarian())
        {
            btnNew.setEnabled(true);
            btnFindOwner.setEnabled(true);
            btnSave.setEnabled(true);
            btnAddCopy.setEnabled(true);
        }
        else
        {
            btnNew.setEnabled(false);
            btnFindOwner.setEnabled(false);
            btnSave.setEnabled(false);
            btnAddCopy.setEnabled(false);
        }
    }

    /**
     * Displays all of the copies of this resource
     */
    void ListCopies()
    {
        String ResourceCode = CurrentResource.ResourceCode(null);
        lstCopies.setModel(new DefaultListModel());
        if (ResourceCode != null || ResourceCode == "")
        {
            ArrayList<VerifiableObject> CopyList = CurrentResource.GetRelatedData(Copy.class, 1000);
            DefaultListModel DLM = new DefaultListModel();
            if (CopyList == null)
                return;
            for(VerifiableObject VO: CopyList)
            {
                if (LoggedMember.getInstance().IsLibrarian() || ((Copy)VO).GoodStatus())
                    DLM.addElement(VO);
            }
            lstCopies.setModel(DLM);
            return;
        }
    }
    
    /**
     * Sets the combobox to the specified Type
     * @param Type The type to set
     */
    public void SetResourceType(String Type)
    {
        if (Type == null)
        {
            Type = (String)cboResourceType.getItemAt(1);//get student
        }
        int Max = cboResourceType.getItemCount();
        for (int itr = 0; itr < Max; itr++)
        {
            if (((String)cboResourceType.getItemAt(itr)).equalsIgnoreCase(Type))
            {
                cboResourceType.setSelectedIndex(itr);
                return;
            }
        }
    }
    
    /**
     * Recreates the resource from the fields
     * @return true if the resource is verified
     */
    boolean RegenerateResource()
    {
        Resource Resource = new Resource();
        
        Resource.ResourceCode(txtResourceCode.getText());
        Resource.Title(txtTitle.getText());
        Resource.SubTitle(txtSubTitle.getText());
        Resource.Keywords(txtKeyword.getText());
        Resource.ForiegnOwner(txtForiegnOwner.getText());
        Resource.Type(cboResourceType.getSelectedItem().toString());
        Resource.Actors(txtActors.getText());
        Resource.Songs(txtSongs.getText());
        Resource.Information(txtInformation.getText());
        Resource.IssueNumber(txtIssueNumber.getText());
        
        ListCopies();
        
        boolean Created = Resource.Verify();
        if(Created)
        {
            CurrentResource = Resource;
        }
        return Created;
    }
    
    /**
     * Creates a new resource
     */
    void NewResource()
    {
        NewResource = true;
        CurrentResource = null;
        FillFields();
    }
    
    /**
     * Saves the current resource
     */
    void Save()
    {
        if (RegenerateResource())
        {
            try
            {
                Connection con = MasterConnection.getInstance();
                Statement stmt = con.createStatement();
                String SQL = "";
                if (NewResource)
                    SQL = CurrentResource.InsertRecrodSQL();
                else
                    SQL = CurrentResource.UpdateRecrodSQL();
                stmt.executeUpdate(SQL);
                
                String Str = "Save successfull.";
                
                if (NewResource)
                    Str = "The resource " + txtTitle.getText() + " has been successfully added to the database.";
                    
                JOptionPane.showMessageDialog(null, Str, "Success", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(Exception Ex)
            {
                JOptionPane.showMessageDialog(null, "Database error, please try again later\n" + Ex.getMessage(), "Database Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
    /**
     * Used to refresh the list of copies after one the user finished with them
     */
    public void RefreshCopies(VerifiableObject VO)
    {
        ListCopies();
    }
    /**
     * Sets the current resource to a specified resource
     * @param VO 
     */
    public void SetResource(VerifiableObject VO)
    {
        if (VO == null)
            return;
        
        CurrentResource = (Resource)VO;
        NewResource = false;
        FillFields();
    }
    /**
     * Sets the foreign Owner of a resource (if null is passed do nothing)
     * @param VO the new foreign owner
     */
    public void SetMember(VerifiableObject VO)
    {
        if (VO == null)
            return;
        
        txtForiegnOwner.setText(((Member)VO).IdentityNumber(null));
    }
    
    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        MainPanel.GetMainPanel().AddCentralUIPanel(new SearchForResource(true), this, SetMethodRef);
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        Save();
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        Close();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        NewResource();
    }//GEN-LAST:event_btnNewActionPerformed

    private void btnFindOwnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindOwnerActionPerformed
        MainPanel.GetMainPanel().AddCentralUIPanel(new SearchForMember(true), this, SetMethodMemberRef);
    }//GEN-LAST:event_btnFindOwnerActionPerformed

    private void btnViewCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewCopyActionPerformed
        VerifiableObject VO = (VerifiableObject)lstCopies.getSelectedValue();
        MainPanel.GetMainPanel().AddCentralUIPanel(new InformationCopy(VO), this, ResetCopiesRef);
    }//GEN-LAST:event_btnViewCopyActionPerformed

    private void btnAddCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddCopyActionPerformed
        String ResCode = txtResourceCode.getText();
        MainPanel.GetMainPanel().AddCentralUIPanel(new InformationCopy(ResCode), this, ResetCopiesRef);
    }//GEN-LAST:event_btnAddCopyActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddCopy;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnFindOwner;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnViewCopy;
    private javax.swing.JComboBox cboResourceType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstCopies;
    private javax.swing.JTextField txtActors;
    private javax.swing.JTextField txtForiegnOwner;
    private javax.swing.JTextField txtInformation;
    private javax.swing.JTextField txtIssueNumber;
    private javax.swing.JTextField txtKeyword;
    private javax.swing.JTextField txtResourceCode;
    private javax.swing.JTextField txtSongs;
    private javax.swing.JTextField txtSubTitle;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
}
